package com.aid.winter.controller;

import com.aid.winter.common.annotation.RequiredRole;
import com.aid.winter.common.constant.RoleConstants;
import com.aid.winter.common.response.Result;
import com.aid.winter.dto.request.BatchCreateRequest;
import com.aid.winter.dto.request.BatchStatusRequest;
import com.aid.winter.dto.request.BatchUpdateRequest;
import com.aid.winter.dto.response.BatchStatisticsVO;
import com.aid.winter.dto.response.BatchVO;
import com.aid.winter.dto.response.PageResult;
import com.aid.winter.service.AidBatchService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 补助批次管理控制器
 */
@RestController
@RequestMapping("/batch")
@RequiredArgsConstructor
public class AidBatchController {

    private final AidBatchService batchService;

    /**
     * 创建批次（仅学校用户和超管可操作）
     */
    @PostMapping
    @RequiredRole({4, 5})
    public Result<BatchVO> createBatch(@Validated @RequestBody BatchCreateRequest request) {
        return Result.ok(batchService.createBatch(request));
    }

    /**
     * 分页查询批次列表
     */
    @GetMapping("/list")
    public Result<PageResult<BatchVO>> listBatches(
            @RequestParam(required = false) String academicYear,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(batchService.listBatches(academicYear, status, pageNum, pageSize));
    }
    
    /**
     * 更新批次
     */
    @PutMapping("/{id}")
    @RequiredRole({4, 5})
    public Result<BatchVO> updateBatch(@PathVariable Long id, @Validated @RequestBody BatchUpdateRequest request) {
        return Result.ok(batchService.updateBatch(id, request));
    }
    
    /**
     * 删除批次
     */
    @DeleteMapping("/{id}")
    @RequiredRole({4, 5})
    public Result<Void> deleteBatch(@PathVariable Long id) {
        batchService.deleteBatch(id);
        return Result.ok();
    }
    
    /**
     * 获取批次详情
     */
    @GetMapping("/{id}")
    public Result<BatchVO> getBatchDetail(@PathVariable Long id) {
        return Result.ok(batchService.getBatchDetail(id));
    }
    
    /**
     * 检查批次是否可用
     */
    @GetMapping("/{id}/check-status")
    public Result<Boolean> checkBatchStatus(@PathVariable Long id) {
        return Result.ok(batchService.checkBatchAvailable(id));
    }
    
    /**
     * 获取批次统计信息
     */
    @GetMapping("/statistics")
    public Result<BatchStatisticsVO> getBatchStatistics() {
        return Result.ok(batchService.getBatchStatistics());
    }
    
    /**
     * 批量更新状态
     */
    @PutMapping("/batch-status")
    @RequiredRole({4, 5})
    public Result<Void> batchUpdateStatus(@RequestBody BatchStatusRequest request) {
        batchService.batchUpdateStatus(request.getIds(), request.getStatus());
        return Result.ok();
    }
    
    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    @RequiredRole({4, 5})
    public Result<Void> batchDelete(@RequestParam List<Long> ids) {
        batchService.batchDelete(ids);
        return Result.ok();
    }
}